package com.csxy.qiu.service.impl;



import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.csxy.qiu.entity.EduCourse;
import com.csxy.qiu.entity.EduCourseDescription;
import com.csxy.qiu.entity.vo.*;
import com.csxy.qiu.exception.MyException;
import com.csxy.qiu.mapper.EduCourseMapper;

import com.csxy.qiu.service.EduChapterService;
import com.csxy.qiu.service.EduCourseService;
import com.csxy.qiu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {


   @Autowired  // 注入课程描述服务
   private EduCourseDescriptionServiceImpl eduCourseDescriptionService;

   @Autowired
   private EduVideoService eduVideoService;

   @Autowired
   private EduChapterService eduChapterService;

    //想课程表里添加课程基本信息的方法
    //CourseInfoForm对象，装换成EduCourse对象
    @Override
    public String addCourseInfo(CourseInfoForm courseInfoForm) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm,eduCourse);
        int result = baseMapper.insert(eduCourse);

        //获取添加之后课程id
        String cid = eduCourse.getId();
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        //手动设置描述课程表的id，与上面的课程信息id关联
        eduCourseDescription.setId(cid);

        return cid;
    }

    //获取课程信息
    @Override
    public CourseInfoForm getCourseInfo(String courseId) {
        //定义一个空的课程对象
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        //根据ID查询课程
        EduCourse eduCourse = baseMapper.selectById(courseId);
        //属性copy
        BeanUtils.copyProperties(eduCourse,courseInfoForm);
        //查询课程简介并赋值
        courseInfoForm.setDescription(eduCourseDescriptionService.getById(courseId).getDescription());
        return courseInfoForm;
    }

    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoForm courseInfoForm) {
        //1.修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm,eduCourse);
        baseMapper.updateById(eduCourse);
        //2.修改描述信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        eduCourseDescription.setId(courseInfoForm.getId());
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }

    //删除课程
    @Override
    public boolean removeCourse(String id) {
    //1.根据课程id删除小节
    eduVideoService.removeVideoByCourseId(id);
    //2.根据课程id删除章节部分
    eduChapterService.removeChapterByCourseId(id);

    //3.根据课程id删除课程描述
    eduCourseDescriptionService.removeById(id);
    //4.根据课程id删除课程本身
    int result = baseMapper.deleteById( id);
    if (result>=1){
        return true;
    }else {
        throw new MyException(20001,"删除课程失败！");
    }
    }

    //条件查询带分页
    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();

        // 使用安全空值检查
        if (courseQuery != null) {
            if (StringUtils.hasText(courseQuery.getTitle())) {
                queryWrapper.like("title", courseQuery.getTitle());
            }
            if (StringUtils.hasText(courseQuery.getStatus())) {
                queryWrapper.eq("status", courseQuery.getStatus());
            }
        }

        queryWrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(pageParam, queryWrapper);
    }

    // 查询课程信息
    @Autowired
    private EduCourseMapper eduCourseMapper;
    @Override

    public CoursePublishVo getPublishCourseInfo(String id) {
        return eduCourseMapper.getPublishCourseInfo(id);
    }

    // 查询课程信息[订单]
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }


    // 查询热门课程,浏览量降序，取前8条
    @Override
//    @Cacheable(key = "'selectHotCourse'",value = "course")
    public List<EduCourse> selectHotCourse() {
        // 查询前8最热门的课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("view_count");
        wrapper.last("limit 8");

        return baseMapper.selectList(wrapper);
    }

    //前端课程查询
    @Override
    public Map<String, Object> getCourseFrontInfo(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {
        // 拼接条件查询
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(courseFrontVo.getSubjectParentId())){    // 一级分类
            wrapper.eq("subject_parent_id", courseFrontVo.getSubjectParentId());
        }
        if (StringUtils.isNotEmpty(courseFrontVo.getSubjectId())){  // 二级分类
            wrapper.eq("subject_id", courseFrontVo.getSubjectId());
        }
        if (StringUtils.isNotEmpty(courseFrontVo.getBuyCountSort())){   //关注度
            wrapper.orderByDesc("buy_count");
        }
        if (StringUtils.isNotEmpty(courseFrontVo.getPriceSort())){  //价格
            wrapper.orderByDesc("price");
        }
        if (StringUtils.isNotEmpty(courseFrontVo.getGmtCreateSort())){   //最新
            wrapper.orderByDesc("gmt_create");
        }
        eduCourseMapper.selectPage(pageCourse, wrapper);
        Map<String, Object> map = new HashMap<>();
        long total = pageCourse.getTotal(); //总记录数
        List<EduCourse> courseList = pageCourse.getRecords();   //数据集合
        long size = pageCourse.getSize();//每页记录数
        long current = pageCourse.getCurrent();//当前页
        long pages = pageCourse.getPages();//总页数
        boolean hasPrevious = pageCourse.hasPrevious();//是否有上一页
        boolean hasNext = pageCourse.hasNext();//是否有下一页
        map.put("total",total);
        map.put("list",courseList);
        map.put("size",size);
        map.put("current",current);
        map.put("pages",pages);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);

        return map;
    }

}
